package cn.dengta.webapp.user.model;

import java.util.ArrayList;
import java.util.List;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.model.ValueProxyImpl;
import cn.dengta.context.model.Language;
import cn.dengta.context.model.Option;
import cn.dengta.webapp.user.entity.Link.LinkConfig;
import cn.dengta.webapp.user.entity.link.LinkPopupConfig;
import lombok.Getter;
import me.codeplayer.util.*;

/**
 * 链接类型
 */
@Getter
public enum LinkType implements ValueProxyImpl<LinkType, String> {

	/** 首页Banner */
	BANNER_TOP("首页Banner", true, LinkCategory.TOP, Language.DEFAULT),
	/** 首页gameBanner */
	TOP_GAME("首页gameBanner", true, LinkCategory.TOP, Language.DEFAULT),
	/** 首页followBanner */
	TOP_FOLLOW("首页followBanner", true, LinkCategory.TOP, Language.DEFAULT),
	/** 动态Banner */
	BANNER_MOMENT("动态Banner", true, LinkCategory.MOMENT, Language.DEFAULT),
	/** 关注动态Banner */
	MOMENT_FOLLOW("关注动态Banner", true, LinkCategory.MOMENT, Language.DEFAULT),
	/** 群广场Banner */
	IM_GROUP("群广场Banner", true, LinkCategory.IM_MSG, Language.DEFAULT),
	/** IM消息Banner */
	BANNER_IM_MSG("IM消息Banner", true, LinkCategory.IM_MSG, Language.DEFAULT),
	/** IM精选Banner */
	IM_TWO("IM精选Banner", true, LinkCategory.IM_MSG, Language.DEFAULT),
	/** IM好友Banner */
	IM_THREE("IM好友Banner", true, LinkCategory.IM_MSG, Language.DEFAULT),
	/** 钱包Banner */
	BANNER_CASH("钱包Banner", true, LinkCategory.CASH, Language.DEFAULT),
	/** 用户中心Banner */
	BANNER_CENTER("用户中心Banner", false, LinkCategory.USER_CENTER, Language.DEFAULT),
	/** 首页悬浮窗 */
	BANNER_TOP_HAND("首页悬浮窗", true, LinkCategory.HAND, Language.DEFAULT, false),
	/** 群悬浮窗 */
	GROUP_HAND("群悬浮窗", true, LinkCategory.HAND, Language.DEFAULT, false),
	/** 聊天悬浮窗 */
	CHAT_HAND("聊天悬浮窗", true, LinkCategory.HAND, Language.DEFAULT, false),
	/** 动态广告 */
	MOMENT_AD("动态广告", true, LinkCategory.AD, Language.DEFAULT),
	/** 进首页弹窗 */
	TOP_POPUP("首页弹窗", true, LinkCategory.POPUP, Language.DEFAULT, LinkPopupConfig.class, false),
	/** 直播banner */
	TOP_LIVE("直播banner", true, LinkCategory.TOP, Language.DEFAULT),
	BANNER_TASK("任务中心banner", true, LinkCategory.TASK, Language.DEFAULT),
	;
	boolean isBanner;
	Language language;
	LinkCategory category;
	boolean show;
	public final Class<? extends LinkConfig> configClazz;
	final ValueProxy<LinkType, String> proxy;

	// 构造函数，枚举类型只能为私有
	LinkType(String label, boolean show, LinkCategory category, Language language, Class<? extends LinkConfig> configClazz, boolean isBanner) {
		this.isBanner = isBanner;
		this.show = show;
		this.category = category;
		this.language = language;
		this.configClazz = configClazz;
		this.proxy = new ValueProxy<>(this, this.name(), label);
	}

	LinkType(String label, boolean show, LinkCategory category) {
		this(label, show, category, null, null, true);
	}

	LinkType(String label, boolean show, LinkCategory category, Language language) {
		this(label, show, category, language, null, true);
	}

	LinkType(String label, boolean show, LinkCategory category, Language language, boolean isBanner) {
		this(label, show, category, language, null, isBanner);
	}

	public static LinkType[] getValues() {
		return ValueProxy.getCachedArray(LinkType.class, LinkType::values);
	}

	public static LinkType getValueByName(String name) {
		for (LinkType type : getValues()) {
			if (type.name().equalsIgnoreCase(name)) {
				return type;
			}
		}
		return null;
	}

	public static List<Option<String>> find(LinkCategory category) {
		return find(category, null);
	}

	public static List<Option<String>> find(LinkCategory category, LinkType filterBasics) {
		final LinkType[] values = getValues();
		List<Option<String>> list = new ArrayList<>(X.size(values));
		for (LinkType value : values) {
			if (filterBasics != null && filterBasics == value) {
				continue;
			}
			if (value.category == category) {
				list.add(new Option<>(StringUtil.toString(value.getValue()), value.getLabel()));
			}
		}
		return list;
	}

	public static LinkType of(String value) {
		return BANNER_TOP.getValueOf(value);
	}

	public String serialize(LinkConfig config) {
		return config == null ? "" : JSONUtil.encode(config);
	}

	public LinkConfig deserialize(String configJSONStr) {
		return StringUtil.isEmpty(configJSONStr) ? null : JSONUtil.parseObject(configJSONStr, configClazz);
	}

}
